package com.magicborrow.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.huanxin.Constant;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/16.
 */
public class AppraiseActivity extends BaseActivity implements View.OnClickListener,VolleyTool.HTTPListener{

    private RatingBar rstar1;
    private RatingBar rstar2;
    private RatingBar rstar3;
    private Button mCommit;
    private int targetType;
    private int targetId;
    private int applyId;
    private int toUserId;
    private float star1;
    private float star2;
    private float star3;
    private EditText etComment;
    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appraise);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        iv_back = (ImageView)findViewById(R.id.iv_back);
        mCommit = (Button)findViewById(R.id.btn_commit);
        etComment = (EditText) findViewById(R.id.tv_comment);
        rstar1 = (RatingBar)findViewById(R.id.rb_miaoshu);
        rstar2 = (RatingBar)findViewById(R.id.rb_zhunshi);
        rstar3 = (RatingBar)findViewById(R.id.rb_taidu);
    }

    private void initData(){
        Bundle bundle = getIntent().getExtras();
        targetType = bundle.getInt("targetType");
        targetId = bundle.getInt("targetId");
        applyId = bundle.getInt("applyId");
        toUserId = bundle.getInt("toUserId");
    }

    private void initListener(){
        iv_back.setOnClickListener(this);
        mCommit.setOnClickListener(this);
    }

    private void sendRequest(){
        star1 = rstar1.getRating();
        star2 = rstar2.getRating();
        star3 = rstar3.getRating();
        String content = etComment.getText().toString();
        if(TextUtils.isEmpty(content)){
            Toast.makeText(this,"评论内容不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String,String> params = new HashMap<>();
        params.put("targetType",targetType+"");
        params.put("targetId",targetId+"");
        params.put("applyId",applyId+"");
        params.put("content",content+"");
        params.put("toUserId",toUserId+"");
        params.put("star1",star1+"");
        params.put("star2",star2+"");
        params.put("star3",star3+"");
        params.put("acc_token", UserTools.getUser(this).getAcc_token());
        VolleyTool.post(Constants.ADD_COMMENT_URL,params,this,0,null);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_commit:
                sendRequest();
                break;
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        try {
            JSONObject obj = new JSONObject(t.toString());
            int code = obj.getInt("code");
            String message = obj.getString("message");
            if(code == 0){
                showShortMsg("评论成功");
                finish();
            }else{
                showShortMsg(message);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        showShortMsg("服务器暂时无法连接");
    }

}
